package com.imooc.freecoding.controller;

import com.imooc.freecoding.entity.News;
import com.imooc.freecoding.service.NewsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
public class NewsController {
    @Resource
    private NewsService newsService;
    @GetMapping("/findById/{id}")
    public Map selectById(@PathVariable Long id) {
        Map result = new HashMap();
        News news = newsService.selectById(id);
        result.put("news", news);
        return result;
    }

    @GetMapping("/insert")
    public Map insertNews(News news) {
        Map result = new HashMap();
        try {
            newsService.insertNews(news);
            result.put("msg", "修改成功");
        } catch (Exception e) {
            result.put("msg", e.getMessage());
        }
        return result;
    }

    @GetMapping("/deleteNews/{id}")
    public Map deleteNews(@PathVariable("id") Long id) {
        Map result = new HashMap();
        try {
            newsService.deleteNews(id);
            result.put("msg", "删除成功");
        } catch (Exception e) {
            result.put("msg", e.getMessage());
        }
        return result;
    }

    @GetMapping("/updateNews")
    public Map updateNews(News news) {
        Map result = new HashMap();
        try {
            newsService.updateNews(news);
            result.put("msg", "修改成功");
        } catch (Exception e) {
            result.put("msg", e.getMessage());
        }
        return result;
    }
}
